// pages/shoudanyoulilingqu/shoudanyoulilingqu.js
const app = getApp();
import { hexMD5 } from "../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    USER_ASSETS_IDCARD: '',
    USER_ASSETS_NAME: '',
    // login_id: '',
    flag: true, //设置节流
    VALID: '',
    openid: '',
    code: '',
    SURPLUS_NUMBER: '',

    // login_idyi: '',
    session_key:'',
  },


  back(){
    wx.navigateBack({
      delta: 1
    });
  },





  keyong(){
    wx.navigateTo({
      url: '../../pages/youpiaokeyong/youpiaokeyong',
    })

  },



  // 支付宝领取

  tjok() {
    if (this.data.flag) {
      this.data.flag = false

      this.setData({
        openid: app.globalData.openid,
        session_key: app.globalData.session_key,
      })
      if (this.data.login_idyi != this.data.login_id) {

        wx.showToast({
          title: '两次账号不一致，请重新输入！',
          icon: 'none',
          duration: 2000
        })
        console.log(111);
      } else {

        wx.request({
          url: app.globalData.interface+'/post/API_1135_sendBirthdayBenefitsALiPay',
          method: 'POST',
          data: {
            USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
            login_id: this.data.login_id,
            KEY: 'xianjgjKey9999',
            session_key:this.data.session_key,
            code: this.data.code,
            openid: this.data.openid,
            OPEN_ID: this.data.openid,
            OPEN_UUID: hexMD5(`${this.data.openid.substring(0,20)}${this.data.session_key.substring(0,10)}`)
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res);
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1
                  });
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            })
          }
        })      
      }
      setTimeout(() => {
        this.data.flag = true
      }, 4500)

    }
  },

  back() {
    wx.navigateBack({
      delta: 1,
    })
  },



  quno() {
    wx.navigateBack({
      delta: 1,
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e)
    var zhunsj = JSON.parse(e.data)
    console.log(zhunsj.data);
    console.log(zhunsj.data.USER_ASSETS_IDCARD);
    this.setData({
      USER_ASSETS_IDCARD: zhunsj.data.USER_ASSETS_IDCARD,
      USER_ASSETS_NAME: zhunsj.data.USER_ASSETS_NAME,
      VALID: zhunsj.data.VALID,
      code: zhunsj.data.code,
      SURPLUS_NUMBER: zhunsj.data.SURPLUS_NUMBER,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) { //wx.login获取code。
          console.log(res.code);
          //发起网络请求

          wx.request({
            url: app.globalData.interface+'/post/API_1062_appGetOpenid',
            // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
            method: 'GET',
            //向后端发送的数据
            data: {
              code: res.code //将code发送到后台服务器。
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
            },
            success: res => {
              console.log(res);
              if (res.data.result != 'success') {
                console.log(1);
              } else {
                console.log(res.data.openid);
                that.setData({
                  openid: res.data.openid
                })

              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})